package de.wwu.ifgi.gischool.ar;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;

import com.wikitude.architect.ArchitectUrlListener;
import com.wikitude.architect.ArchitectView;

import de.wwu.ifgi.gischool.ar.app.ArApplication;
import de.wwu.ifgi.gischool.ar.pojo.PoiBean;

public class WikiActivity extends Activity implements ArchitectUrlListener,
	LocationListener {

    private ArchitectView architectView;
    private ArrayList<PoiBean> poiBeanList;
    private LocationManager locationManager;
    private String apiKey = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);

	architectView = new ArchitectView(this);

	setContentView(architectView);

	architectView.onCreate(apiKey);

    }

    private void loadWorld() throws JSONException, IOException {
	this.architectView.load("wiki_layout.html");

	ArApplication arApplication = (ArApplication) getApplication();
	poiBeanList = arApplication.getPoiBeans();

	JSONArray jsonArray = new JSONArray();

	for (PoiBean poiBean : poiBeanList) {
	    jsonArray.put(poiBean.toJSONObject());
	}

	this.architectView.callJavascript("newData(" + jsonArray.toString()
		+ ");");
    }

    @Override
    protected void onPause() {
	super.onPause();

	architectView.onPause();
	locationManager.removeUpdates(this);
    }

    @Override
    protected void onResume() {
	super.onResume();

	architectView.onResume();

	locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
		0, this);

	Location lastKnownLocation = locationManager
		.getLastKnownLocation(LocationManager.GPS_PROVIDER);
	if (lastKnownLocation != null) {
	    this.architectView.setLocation(lastKnownLocation.getLatitude(),
		    lastKnownLocation.getLongitude(),
		    (float) lastKnownLocation.getAltitude());
	}

	// architectView.setLocation(51.975946, 7.633339, 50.0f);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
	super.onPostCreate(savedInstanceState);

	architectView.onPostCreate();
	architectView.registerUrlListener(this);

	try {
	    loadWorld();
	} catch (JSONException e) {
	    Log.e("wichtig", e.toString());
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    @Override
    protected void onDestroy() {
	super.onDestroy();

	architectView.onDestroy();
    }

    @Override
    public void onLowMemory() {
	super.onLowMemory();

	architectView.onLowMemory();
    }

    @Override
    public boolean urlWasInvoked(String url) {

	List<NameValuePair> queryParams = URLEncodedUtils.parse(
		URI.create(url), "UTF-8");

	String id = "";
	// getting the values of the contained GET-parameters
	for (NameValuePair pair : queryParams) {
	    if (pair.getName().equals("id")) {
		id = pair.getValue();
	    }
	}

	ArApplication arApplication = (ArApplication) getApplication();
	arApplication.setSelectedPOI(id);

	Intent intent = new Intent(this, DetailActivity.class);
	startActivity(intent);

	return true;
    }

    @Override
    public void onLocationChanged(Location location) {
	if (architectView != null)
	    architectView.setLocation(location.getLatitude(),
		    location.getLongitude(), (float) location.getAltitude());
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

}
